function(CheckAndUpdateFile FILENAME CONTENT)
	if (EXISTS ${FILENAME})
		file(READ ${FILENAME} _CUR)
	else ()
		file(WRITE ${FILENAME} "")
		set(_CUR "")
	endif ()
	if (NOT _CUR STREQUAL CONTENT)
		message(STATUS "  Updating ${FILENAME}...")
		file(WRITE ${FILENAME} ${CONTENT})
	else ()
		message(STATUS "  ${FILENAME} is up to date.")
	endif()
endfunction()

macro(GenReflectionHeaderListFromSourceFiles TARGETNAME)
	message(STATUS "Creating reflection build files for target ${TARGETNAME}")
	set(_FILE_CONTENTS "")
	set(_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/Generated/${TARGETNAME}.reflection")
	foreach(_FILE ${ARGN})
		get_filename_component(_EXT ${_FILE} EXT)
		if (_EXT STREQUAL ".h")
			set(_FILE_CONTENTS "${_FILE_CONTENTS}\#include \"${_FILE}\"\n")
		endif()
	endforeach()
	CheckAndUpdateFile(${_FILENAME} ${_FILE_CONTENTS})
endmacro()

macro(GenReflectionDirListFromSourceDirs TARGETNAME)
	message(STATUS "Creating reflection Directorys in cmd args for target ${TARGETNAME}")
	set(_FILE_CONTENTS "")
	set(_FILENAME "${CMAKE_CURRENT_SOURCE_DIR}/Generated/${TARGETNAME}.dirs")
	foreach(_FILE ${ARGN})
		set(_FILE_CONTENTS "${_FILE_CONTENTS}-I${_FILE}\n")
	endforeach()
	CheckAndUpdateFile(${_FILENAME} ${_FILE_CONTENTS})
endmacro()

function(ToAbsPath PathVar Prefix)
    set(PathVal ${${PathVar}})
    string(REGEX REPLACE "\\\\" "/" PathVal "${PathVal}" )
    if(NOT "${PathVal}" MATCHES "^ *[A-Za-z]:/")
        if("${PathVal}" MATCHES "^ *\\./")
            string(REGEX REPLACE "^ *\\./" "" PathVal ${PathVal})
        endif()
        set(${PathVar} "${Prefix}/${PathVal}" PARENT_SCOPE)
    endif()
endfunction()

function(CollectLocalIncludeDirs VarName DirList)
	set(LocalDirs "")
	foreach(Dir ${DirList})
		ToAbsPath(Dir "${CMAKE_CURRENT_SOURCE_DIR}")
		set(LocalDirs ${LocalDirs} ${Dir})
	endforeach()
	set(${VarName}  ${LocalDirs} PARENT_SCOPE)
endfunction()

macro(GenHeaderFileList)
	set(${PROJECT_NAME}_Header_Files "")
	foreach(_FILE ${ARGN})
		get_filename_component(_EXT ${_FILE} EXT)
		if (_EXT STREQUAL ".h")
			list(APPEND ${PROJECT_NAME}_Header_Files ${_FILE})
		endif()
	endforeach()
endmacro()